﻿function addOrEditProblem(pid) {
	if (pid) {
		if (refreshTab('editProblem' + pid)) return;
		getProblemCompleteInfo({
					pid : pid,
					success : onload
				});
	} else
		onload({});

	function onload(data) {
		/* region TextField */
		var txtpid = new Ext.form.NumberField({
					fieldLabel : 'PID',
					name : 'pid',
					anchor : '100%',
					maxLength : 10,
					allowBlank : false,
					allowDecimals : false,
					allowNegative : false,
					value : data.pid
				});
		var autopid = new Ext.Button({
					text : '自动',
					anchor : '100%',
					handler : onAutoPid
				});
		var title = new Ext.form.TextField({
					fieldLabel : '标题',
					name : 'title',
					maxLength : 200,
					allowBlank : false,
					value : data.title
				});
		var timelimit = new Ext.form.NumberField({
					fieldLabel : '时间限制(MS)',
					name : 'timeLimit',
					anchor : '100%',
					maxLength : 10,
					allowBlank : false,
					allowDecimals : false,
					allowNegative : false,
					value : data.timeLimit || 1000
				});
		var memlimit = new Ext.form.NumberField({
					fieldLabel : '内存限制(KB)',
					name : 'memoryLimit',
					anchor : '100%',
					maxLength : 10,
					allowBlank : false,
					allowDecimals : false,
					allowNegative : false,
					value : data.memoryLimit || 65536
				});
		var disa = new Ext.form.Checkbox({
					boxLabel : 'Disabled',
					name : 'disabled',
					checked : data.disabled
				});
		var desc = new Ext.ux.AdvHtmlEditor({
					fieldLabel : '描述',
					name : 'description',
					value : data.description
				});
		var input = new Ext.ux.AdvHtmlEditor({
					fieldLabel : '输入',
					name : 'input',
					value : data.input
				});
		var output = new Ext.ux.AdvHtmlEditor({
					fieldLabel : '输出',
					name : 'output',
					value : data.output
				});
		var saminput = new Ext.form.TextArea({
					fieldLabel : '样例输入',
					name : 'sampleInput',
					height : 300,
					allowBlank : false,
					value : data.sampleInput
				});
		var samoutput = new Ext.form.TextArea({
					fieldLabel : '样例输出',
					name : 'sampleOutput',
					height : 300,
					allowBlank : false,
					value : data.sampleOutput
				});
		var hit = new Ext.ux.AdvHtmlEditor({
					fieldLabel : '提示',
					name : 'hint',
					value : data.hint
				});
		var source = new Ext.form.TextField({
					fieldLabel : '来源',
					name : 'source',
					maxLength : 200,
					allowBlank : true,
					value : data.source
				});
		var exampleLang = new Ext.form.ComboBox({
					fieldLabel : '样例程序语言',
					hiddenName : 'exampleLang',
					mode : 'local',
					triggerAction : 'all',
					valueField : 'code',
					displayField : 'title',
					editable : false,
					allowBlank : true,
					store : new Ext.data.Store({
								autoLoad : true,
								url : url.getLangList,
								reader : new Ext.data.JsonReader({
											root : 'result',
											id : 'code'
										}, ['code', 'title']),
								listeners : {
									load : function() {
										exampleLang.setValue(data.exampleLang);
									}
								}
							}),
					validator : function(v) {
						if (Ext.isEmpty(exampleCode.getValue()) || !Ext.isEmpty(v)) return true;
						return '请选择';
					}
				});
		var exampleCode = new Ext.form.TextArea({
					fieldLabel : '样例程序',
					name : 'exampleCode',
					height : 300,
					allowBlank : true,
					value : data.exampleCode,
					validator : function(v) {
						if (Ext.isEmpty(exampleLang.getValue()) || !Ext.isEmpty(v)) return true;
						return '请输入内容';
					}
				});
		var inputdata = new Ext.form.TextArea({
					fieldLabel : '输入数据',
					height : 300,
					allowBlank : true,
					value : data.inputData,
					validator : function(v) {
						if (datajudge.collapsed || !Ext.isEmpty(v)) return true;
						return '请输入内容';
					}
				});
		var outputdata = new Ext.form.TextArea({
					fieldLabel : '输出数据',
					height : 300,
					allowBlank : true,
					value : data.outputData,
					validator : function(v) {
						if (datajudge.collapsed || !Ext.isEmpty(v)) return true;
						return '请输入内容';
					}
				});
		var inputdatafile = new Ext.ux.FileUploadField({
					fieldLabel : '输入数据文件',
					buttonText : '选择输入文件...',
					name : 'inputDataFile',
					allowBlank : true,
					validator : function(v) {
						if (filejudge.collapsed || !Ext.isEmpty(v)) return true;
						return '请选择文件';
					}
				});
		var outputdatafile = new Ext.ux.FileUploadField({
					fieldLabel : '输出数据文件',
					buttonText : '选择输出文件...',
					name : 'outputDataFile',
					allowBlank : true,
					validator : function(v) {
						if (filejudge.collapsed || !Ext.isEmpty(v)) return true;
						return '请选择文件';
					}
				});
		var spcinputdata = new Ext.form.TextArea({
					fieldLabel : '输入数据',
					height : 300,
					allowBlank : true,
					value : data.specialJudge ? data.inputData : '',
					validator : function(v) {
						if (spcjudge.collapsed || !Ext.isEmpty(v)) return true;
						return '请输入内容';
					}
				});
		var spccode = new Ext.form.TextArea({
					fieldLabel : '评判程序',
					name : 'specialCode',
					height : 300,
					allowBlank : true,
					value : data.specialCode,
					validator : function(v) {
						if (spcjudge.collapsed || !Ext.isEmpty(v)) return true;
						return '请输入内容';
					}
				});
		var spclang = new Ext.form.ComboBox({
					fieldLabel : '程序语言',
					hiddenName : 'specialLang',
					mode : 'local',
					triggerAction : 'all',
					valueField : 'code',
					displayField : 'title',
					editable : false,
					allowBlank : true,
					store : new Ext.data.Store({
								autoLoad : true,
								url : url.getLangList,
								reader : new Ext.data.JsonReader({
											root : 'result',
											id : 'code'
										}, ['code', 'title']),
								listeners : {
									load : function() {
										spclang.setValue(data.specialLang);
									}
								}
							}),
					validator : function(v) {
						if (spcjudge.collapsed || !Ext.isEmpty(v)) return true;
						return '请选择';
					}
				});
		var spcoutputdata = new Ext.form.TextArea({
					fieldLabel : '评判程序输出',
					height : 300,
					allowBlank : true,
					value : data.specialJudge ? data.outputData : '',
					validator : function(v) {
						if (spcjudge.collapsed || !Ext.isEmpty(v)) return true;
						return '请输入内容';
					}
				});
		/* endregion */

		/* region FieldSet */
		var baseinfo = new Ext.form.FieldSet({
					title : '题目描述',
					collapsible : true,
					defaults : {
						anchor : '95%'
					},
					items : [{
								layout : 'column',
								border : false,
								items : [{
											layout : 'form',
											columnWidth : .9,
											border : false,
											bodyStyle : 'padding-right:5px',
											items : txtpid
										}, {
											layout : 'form',
											columnWidth : .1,
											border : false,
											items : autopid
										}]
							}, title, {
								layout : 'column',
								border : false,
								items : [{
											layout : 'form',
											columnWidth : .5,
											border : false,
											items : timelimit
										}, {
											layout : 'form',
											columnWidth : .5,
											border : false,
											items : memlimit
										}]
							}, desc, input, output, saminput, samoutput, hit, source, exampleLang,
							exampleCode, disa]
				});
		var datajudge = new Ext.form.FieldSet({
					title : '对比评判',
					checkboxToggle : true,
					collapsed : data.specialJudge,
					collapsible : false,
					defaults : {
						anchor : '95%'
					},
					items : [inputdata, outputdata]
				});
		datajudge.on('expand', function() {
					filejudge.collapse();
					spcjudge.collapse();
				});
		var filejudge = new Ext.form.FieldSet({
					title : '对比评判（上传文件）',
					checkboxName : 'uploadDataFile',
					checkboxToggle : true,
					collapsed : true,
					collapsible : false,
					defaults : {
						anchor : '95%'
					},
					items : [inputdatafile, outputdatafile]
				});
		filejudge.on('expand', function() {
					datajudge.collapse();
					spcjudge.collapse();
				});
		var spcjudge = new Ext.form.FieldSet({
					title : '程序评判',
					checkboxName : 'specialJudge',
					checkboxToggle : true,
					collapsed : !data.specialJudge,
					collapsible : false,
					defaults : {
						anchor : '95%'
					},
					items : [spcinputdata, spccode, spclang, spcoutputdata]
				});
		spcjudge.on('expand', function() {
					datajudge.collapse();
					filejudge.collapse();
				});

		var form = new Ext.FormPanel({
					id : data.pid ? 'editProblem' + data.pid : 'addProblem' + Ext.id(),
					title : data.pid ? '编辑题目-' + data.pid : '添加题目',
					tabTip : data.pid ? data.pid + ' - ' + data.title : '添加题目',
					closable : true,
					autoScroll : true,
					fileUpload : true,
					labelAlign : 'right',
					labelWidth : 80,
					bodyStyle : 'padding: 20px 40px 20px 20px',
					items : [baseinfo, datajudge, filejudge, spcjudge],
					fbar : new Ext.Toolbar({
								items : [new Ext.Button({
													text : '重置',
													iconCls : 'reset-icon',
													handler : onReset
												}), new Ext.Button({
													text : '提交',
													iconCls : 'ok-icon',
													handler : onSubmit
												})]
							})
				});
		addTab(form);
		if (!data.pid) onAutoPid();
		/* endregion */

		/* region Event */
		function onReset() {
			form.getForm().reset();
		}
		function onSubmit() {
			if (!form.getForm().isValid() || !isNoBlank([desc, input, output])) {
				alertx('请正确输入数据！');
				return;
			}
			if (datajudge.collapsed && filejudge.collapsed && spcjudge.collapsed) {
				alertx('至少选择一种评判机制！');
				return;
			}
			var params = {};
			if (pid) params.oldPid = pid;
			if (spcjudge.collapsed) {
				params.inputData = inputdata.getValue();
				params.outputData = outputdata.getValue();
			} else {
				params.inputData = spcinputdata.getValue();
				params.outputData = spcoutputdata.getValue();
			}
			ajaxform({
						form : form,
						url : url.addOrEditProblem,
						params : params,
						success : function() {
							showProblem(txtpid.getValue());
							refreshProblemList();
							closeTab(form.id);
						}
					});
		}
		function onAutoPid() {
			getAutoPid(function(obj) {
						txtpid.setValue(obj.pid);
						title.focus(true, 200);
					});
		}
		/* endregion */
	}
}

function getProblemCompleteInfo(obj) {
	ajax({
				url : url.getProblemCompleteInfo,
				params : {
					pid : obj.pid
				},
				success : obj.success
			});
}

function getAutoPid(success) {
	ajax({
				url : url.getAutoPid,
				success : success
			});
}
